#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "System.h"
#include "Define.h"
#include "Ports.h"

#define ProperCode //testby simperfive

#ifdef LOLCode
FILE *fp;
#endif

#if 0

int main(int argc, char** argv)
{
	#ifdef LOLCode
	printf("Trying to set up GPIO4\n");

	//To hold commands to send
	char set_value[4];
	//To make port go toggle
	int toggle = 0;
	//Check if the export file to create the GPIO pin is available
	if((fp = fopen("/sys/class/gpio/export","ab")) == NULL)
	{
		printf("wtf did we do to export :(\n");
		exit(1);
	}
	//Go to the start of the file
	rewind(fp);
	//Copy the value 4 to set_value
	strcpy(set_value, "4");
	//Write it to the file
	fwrite(&set_value, sizeof(char), 1, fp);
	//Close the file or DIE
	fclose(fp);
	//Print that success have been achieved!
	printf("/sys/class/gpio/gpio4 should be created\n");
	//Lets set the direction of the pin
	//First lets check if the direction file exists
	if((fp = fopen("/sys/class/gpio/gpio4/direction","ab")) == NULL)
	{
		printf("You killed Direction...\n");
		exit(1);
	}
	//Go the start of the file
	rewind(fp);
	//Copy "out" to set_value
	strcpy(set_value, "out");
	//Write out set_value to the file
	fwrite(&set_value, sizeof(char), 3, fp);
	//Remember to close the file, or DIE
	fclose(fp);
	//Print out success!
	printf("Direction of pin set to OUT\n");

//	//Go to the start of the file
//	rewind(fp);
//	//Copy 1 to the set_value
//	strcpy(set_value, "1");
//	//Write this to the file
//	fwrite(&set_value, sizeof(char), 1, fp);
//	//Close the file
//	fclose(fp);
//	printf("Value set to 1\n");

	//Time to make a loop that toggles GPIO pin 4 every second
	while(1)
	{
		//Now lets set the initial value of the pin...
		if((fp = fopen("/sys/class/gpio/gpio4/value", "ab")) == NULL)
		{
			printf("Can't set value, I would blame Canada!\n");
			exit(1);
		}
		if(toggle == 1)
		{
			//Go to the start of the file
			rewind(fp);
			//Copy 1 to the set_value
			strcpy(set_value, "1");
			//Write this to the file
			fwrite(&set_value, sizeof(char), 1, fp);
			//Close the file
			fclose(fp);
			printf("Value set to 1\n");
			toggle = 0;
		}
		else if(toggle == 0)
		{
			//Go to the start of the file
			rewind(fp);
			//Copy 1 to the set_value
			strcpy(set_value, "0");
			//Write this to the file
			fwrite(&set_value, sizeof(char), 1, fp);
			//Close the file
			fclose(fp);
			printf("Value set to 0\n");
			toggle = 1;
		}
		sleep(1);
	}
	#endif

	#ifdef ProperCode
	System_CreateGpioPin(PA04, defPinOutput, defPinHigh);
	System_CreateGpioPin(PA01, defPinOutput, defPinLow);
	printf("Now lets try to remove PA01!\n");
	System_RemoveGpioPin(PA01);
	while(1)
	{
//		if(System_ReadValue(PA04) == 1)
//		{
//			System_SetValue(PA04, defPinLow);
//		}
//		else if(System_ReadValue(PA04) == 0)
//		{
//			System_SetValue(PA04, defPinHigh);
//		}
		System_TurnOnLed(defLedA);
		System_TurnOffLed(defLedB);
		sleep(1);
		System_TurnOffLed(defLedA);
		System_TurnOnLed(defLedB);
		sleep(1);
	}
	#endif

	return 0;
}

#endif
